package networkcontroller.xml;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import dataTypes.File;

/**
 * class describing result packet
 */
public class XMLResult extends XML
{
	/**
	 * creates xml result
	 * 
	 * @param host
	 *            source host
	 * @param port
	 *            source host port
	 * @param id
	 *            id of search
	 * @param fileList
	 *            list of files associated with search
	 */
	public XMLResult(String host, int port, int id, Iterable<File> fileList)
	{
		/*
		 * <result> <sender> <host></host> <port></port> </sender> <id></id>
		 * <file> <name></name> <size></size> <sum></sum> </file <result>
		 */
		super();
		createNewDocument();
		Node resultNode = document.createElement("result");
		document.appendChild(resultNode);
		Node senderNode = document.createElement("sender");
		resultNode.appendChild(senderNode);
		Node hostNode = document.createElement("host");
		hostNode.setTextContent(host);
		senderNode.appendChild(hostNode);
		Node portNode = document.createElement("port");
		portNode.setTextContent(String.valueOf(port));
		senderNode.appendChild(portNode);
		Node idNode = document.createElement("id");
		idNode.setTextContent(String.valueOf(id));
		resultNode.appendChild(idNode);
		Iterator<File> fileIter = fileList.iterator();
		while (fileIter.hasNext())
		{
			File file = fileIter.next();
			Node fileNode = document.createElement("file");
			resultNode.appendChild(fileNode);
			Node fileNameNode = document.createElement("filename");
			fileNameNode.setTextContent(file.getPath());
			fileNode.appendChild(fileNameNode);
			Node sizeNode = document.createElement("size");
			sizeNode.setTextContent(String.valueOf(file.getSize()));
			fileNode.appendChild(sizeNode);
			Node sumNode = document.createElement("sum");
			sumNode.setTextContent(file.getHash());
			fileNode.appendChild(sumNode);
		}
	}

	/**
	 * creates xml result from data
	 * 
	 * @param XMLData
	 *            byte array of data
	 */
	public XMLResult(byte[] XMLData)
	{
		super(XMLData);
	}

	/**
	 * gets source host
	 * 
	 * @return source host
	 */
	public String getHost()
	{
		NodeList hosts = document.getElementsByTagName("host");
		return hosts.item(0).getTextContent();
	}

	/**
	 * gets source host port
	 * 
	 * @return source host port
	 */
	public int getPort()
	{
		NodeList ports = document.getElementsByTagName("port");
		return Integer.parseInt(ports.item(0).getTextContent());
	}

	/**
	 * gets id of search associated with results
	 * 
	 * @return id of search
	 */
	public int getId()
	{
		NodeList ids = document.getElementsByTagName("id");
		return Integer.parseInt(ids.item(0).getTextContent());
	}

	/**
	 * gets file list of results
	 * 
	 * @return results file list
	 */
	public List<File> getFileList()
	{
		LinkedList<File> fileList = new LinkedList<File>();
		NodeList files = document.getElementsByTagName("file");
		if (files != null)
			for (int index = 0; index < files.getLength(); index++)
			{
				Node result = files.item(index);
				NodeList childs = result.getChildNodes();
				String filename = null;
				int size = 0;
				String sum = null;
				for (int index2 = 0; index2 < childs.getLength(); index2++)
				{
					Node child = childs.item(index2);
					System.out.println(child.getNodeName());
					if (child.getNodeName().equals("filename"))
					{
						filename = child.getTextContent();
					} else if (child.getNodeName().equals("size"))
					{
						size = Integer.valueOf(child.getTextContent());
					} else if (child.getNodeName().equals("sum"))
					{
						sum = child.getTextContent();
					}
				}
				fileList.add(new File(filename, size, sum));
			}
		return fileList;
	}
}
